//
//  MCButton.m
//  ButtonStyleDemo
//
//  Created by 刘志伟 on 2017/6/16.
//  Copyright © 2017年 刘志伟. All rights reserved.
//
#define scale 0.6
#define titleFont [UIFont systemFontOfSize:18.0]

#import "MCButton.h"
@implementation MCButton

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel.font = titleFont;
        self.buttonStyle = imageLeft;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        //自定义
//         [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [self setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
//        UIImage *imageNor = [UIImage imageNamed:@"btn_nor_xiaosanjiao"];
//        UIImage *imageHig = [UIImage imageNamed:@"btn_click_xiaosanjiao"];
//        [self setImage:[UIImage imageNamed:@"btn_choose_xiaosanjiao"] forState:UIControlStateHighlighted];
//        [self setImage:imageNor forState:UIControlStateNormal];
//        [self setImage:imageHig forState:UIControlStateSelected];
    }
    
    return self;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        self.titleLabel.font = titleFont;
        self.buttonStyle = imageLeft;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        //自定义
//        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [self setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
//        UIImage *imageNor = [UIImage imageNamed:@"btn_nor_xiaosanjiao"];
//        UIImage *imageHig = [UIImage imageNamed:@"btn_click_xiaosanjiao"];
//        [self setImage:imageNor forState:UIControlStateNormal];
//        [self setImage:imageHig forState:UIControlStateSelected];
    }
    
    return self;
}
//
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
    if (self.buttonStyle == imageRight) {
        return [self imageRectWithImageRightForContentTect:contentRect];
    }else if (self.buttonStyle == imageTop){
        return [self imageRectWithImageTopForContentTect:contentRect];
    }else if (self.buttonStyle == imageTabBarTop){
        return [self imageRectWithImageTabBarTopForContentTect:contentRect];
    }else if (self.buttonStyle == imageUpperRight){
        return [self imageRectWithImageUpperRightForContentTect:contentRect];
    }else if (self.buttonStyle == imageCustomeTop){
        return [self imageRectWithimageCustomeTopForContentTect:contentRect];
    }else if (self.buttonStyle == imageBottom){
        return [self imageRectWithImageBottomForContentTect:contentRect];
    }else if (self.buttonStyle == imageNormalBottom){
        return [self imageRectWithImageNormalBottomForContentTect:contentRect];
    }else if (self.buttonStyle == imageLeft){
        return [self imageRectWithImageLeftForContentTect:contentRect];
    }else if (self.buttonStyle == imageCustomeLeft){
        return [self imageRectWithImageCustomeLeftForContentTect:contentRect];
    }else if (self.buttonStyle == imageMarginRight) {
        return [self imageRectWithImageMarginRightForContentTect:contentRect];
    } else {
        return [self imageRectWithImageNormalRightForContentTect:contentRect];
    }

}

- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    if (self.buttonStyle == imageRight) {
        return [self titleRectWithImageRightForContentTect:contentRect];
    }else if (self.buttonStyle == imageTop){
        return [self titleRectWithImageTopForContentTect:contentRect];
    }else if (self.buttonStyle == imageTabBarTop){
        return [self titleRectWithImageTabBarTopForContentTect:contentRect];
    }else if (self.buttonStyle == imageUpperRight){
        return [self titleRectWithImageUpperRightForContentTect:contentRect];
    }else if (self.buttonStyle == imageCustomeTop){
        return [self titleRectWithImageCustomeTopForContentTect:contentRect];
    }else if (self.buttonStyle == imageBottom){
        return [self titleRectWithImageBottomForContentTect:contentRect];
    }else if (self.buttonStyle == imageNormalBottom){
        return [self titleRectWithImageNormalBottomForContentTect:contentRect];
    }else if (self.buttonStyle == imageLeft){
        return [self titleRectWithImageLeftForContentTect:contentRect];
    }else if (self.buttonStyle == imageCustomeLeft){
        return [self titleRectWithImageCustomeLeftForContentTect:contentRect];
    }else if (self.buttonStyle == imageMarginRight) {
        return [self titleRectWithImageMarginRightForContentTect:contentRect];
    }else {
        return [self titleRectWithImageNormalRightForContentTect:contentRect];
    }
}
#pragma mark imageTop 图片在右上角
- (CGRect)imageRectWithImageUpperRightForContentTect:(CGRect)contentRect{
    CGFloat imageWH = 8;
//    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat imageY = 0;
    CGFloat imageX = (CGRectGetWidth(contentRect) - imageWH);
    CGRect rect = CGRectMake(imageX, imageY, imageWH, imageWH);
    
    return rect;
}
#pragma mark imageTop 图片在右上角 文字在下
- (CGRect)titleRectWithImageUpperRightForContentTect:(CGRect)contentRect{
    CGFloat imageWH =8;
//    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect]-imageWH;
    CGFloat titleH = 22;
    CGFloat titleY = imageWH;
    
    CGRect rect = CGRectMake(0, imageWH/2, CGRectGetWidth(contentRect) , titleH);
    
    return rect;
}

#pragma mark imageTop 图片在上 文字在下
- (CGRect)imageRectWithImageTopForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)/3;
    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat imageY = (CGRectGetHeight(contentRect)-imageWH-titleH)/2;
    CGFloat imageX = (CGRectGetWidth(contentRect) - imageWH)/2;
    CGRect rect = CGRectMake(imageX, imageY, imageWH, imageWH);
    
    return rect;
}
#pragma mark imageTop 图片在上 文字在下//imageTabBarTop
- (CGRect)imageRectWithImageTabBarTopForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)/1.4;
    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat imageY = -2;
    CGFloat imageX = (CGRectGetWidth(contentRect) - imageWH)/2;
    CGRect rect = CGRectMake(imageX, imageY, imageWH, imageWH);
    
    return rect;
}
#pragma mark imageTop 图片在上 文字在下//ImageTabBarTop
- (CGRect)titleRectWithImageTabBarTopForContentTect:(CGRect)contentRect{
//    CGFloat imageWH = CGRectGetHeight(contentRect)/1.4;
//    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
//    CGFloat titleY = (CGRectGetHeight(contentRect)-imageWH-titleH)/2+imageWH;
//
//    CGRect rect = CGRectMake(0, titleY, CGRectGetWidth(contentRect) , titleH);
//
//    return rect;
    CGFloat imageWH = CGRectGetHeight(contentRect)/2;
    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat titleY = (CGRectGetHeight(contentRect)-imageWH-titleH)/2+imageWH+5;
    
    CGRect rect = CGRectMake(0, titleY, CGRectGetWidth(contentRect) , titleH);
    
    return rect;
}
- (CGRect)imageRectWithimageCustomeTopForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)/2;
    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat imageY = (CGRectGetHeight(contentRect)-imageWH-titleH)/2;
    CGFloat imageX = (CGRectGetWidth(contentRect) - imageWH)/2;
    CGRect rect = CGRectMake(imageX, imageY, imageWH, imageWH);
    return rect;
}
- (CGRect)titleRectWithImageCustomeTopForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)/2;
    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat titleY = (CGRectGetHeight(contentRect)-imageWH-titleH)/2+imageWH+5;

    CGRect rect = CGRectMake(0, titleY, CGRectGetWidth(contentRect) , titleH);
    
    return rect;
}
- (CGRect)titleRectWithImageTopForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)/2*scale;
    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat titleY = (CGRectGetHeight(contentRect)-imageWH-titleH)/2+(CGRectGetHeight(contentRect)-imageWH-titleH)/4+imageWH;
    
    CGRect rect = CGRectMake(0, titleY, CGRectGetWidth(contentRect) , titleH);
    
    return rect;
}

#pragma mark imageLeft 图片在左 文字在右
- (CGRect)imageRectWithImageLeftForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)*scale;
    CGFloat inteval = (CGRectGetHeight(contentRect)-imageWH)/2;
    CGFloat titleW = [self widthForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat imageX = (CGRectGetWidth(contentRect)-titleW-imageWH)/2;
    if (imageX < 0) {
        imageX = 0;
    }
    CGRect rect = CGRectMake(imageX, inteval, imageWH, imageWH);
    
    return rect;
}

- (CGRect)titleRectWithImageLeftForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)*scale;
    CGFloat titleW = [self widthForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat imageX = (CGRectGetWidth(contentRect)-titleW-imageWH)/2;
    if (imageX < 0) {
        imageX = 0;
    }
    CGRect rect = CGRectMake(imageWH+imageX, 0, titleW , CGRectGetHeight(contentRect));
    
    return rect;
}
//
#pragma mark imageCustomeLeft 图片在左 文字在右
- (CGRect)imageRectWithImageCustomeLeftForContentTect:(CGRect)contentRect{
    CGFloat imageWH = 20;
    CGFloat inteval = (CGRectGetHeight(contentRect)-imageWH)/2;
//    CGFloat titleW = [self widthForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat titleW = [self getWidthWithTitle:self.currentTitle withFontSize:16];
    XLog(@"titleW:%f",titleW);
    CGFloat imageX = (CGRectGetWidth(contentRect)-imageWH/2-titleW/2)/2;
    if (imageX < 0) {
        imageX = 0;
    }
    CGRect rect = CGRectMake(imageX, inteval, imageWH, imageWH);
    
    return rect;
}

- (CGRect)titleRectWithImageCustomeLeftForContentTect:(CGRect)contentRect{
    CGFloat imageWH = 20;
    CGFloat titleW = [self getWidthWithTitle:self.currentTitle withFontSize:16];
    CGFloat imageX = (CGRectGetWidth(contentRect)-imageWH/2-titleW/2)/2;
    if (imageX < 0) {
        imageX = 0;
    }
//    CGRect rect = CGRectMake(imageWH+imageX+10, 0, CGRectGetWidth(contentRect)-(imageWH+imageX+10) , CGRectGetHeight(contentRect));
    CGRect rect = CGRectMake(imageWH+imageX+10, 0, titleW , CGRectGetHeight(contentRect));
    
    return rect;
}
#pragma mark imageBottom 图片在下 文字在上
- (CGRect)imageRectWithImageBottomForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)/2*scale;
    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat imageY = (CGRectGetHeight(contentRect)-imageWH-titleH)/2+titleH;
    CGFloat imageX = (CGRectGetWidth(contentRect) - imageWH)/2;
    CGRect rect = CGRectMake(imageX, imageY, imageWH, imageWH);
    
//    return rect;
#warning 我自定义
    CGFloat titleW = [self getWidthWithTitle:[self currentTitle] withFontSize:13];
    CGFloat w = kUIScreen.size.width/3;
    CGFloat x = (titleW)/2;
    CGFloat y = (CGRectGetHeight(contentRect)) -w-5;
    CGFloat h = w;
//    return CGRectMake((CGRectGetWidth(contentRect)-20)/2, CGRectGetHeight(contentRect)-2, 20, 2);
    return CGRectMake((CGRectGetWidth(contentRect)-20)/2, 38, 20, 2);
}

- (CGRect)titleRectWithImageBottomForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)/2*scale;
    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
//    CGFloat titleY = (CGRectGetHeight(contentRect)-imageWH-titleH)/2;
    #warning 自定义
    CGFloat titleY = (CGRectGetHeight(contentRect)-titleH)/2-10;
    CGRect rect = CGRectMake(0, titleY, CGRectGetWidth(contentRect) , 39);
    
    return rect;
}
//imageRectWithImageNormalBottomForContentTect
- (CGRect)imageRectWithImageNormalBottomForContentTect:(CGRect)contentRect{
    CGFloat titleW = [self getWidthWithTitle:[self currentTitle] withFontSize:14]-10;
    CGFloat imageWH = CGRectGetHeight(contentRect)/2*scale;
    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat imageY = (CGRectGetHeight(contentRect)-imageWH-titleH)/2+titleH;
    CGFloat imageX = (CGRectGetWidth(contentRect) - titleW)/2;
    CGRect rect = CGRectMake(imageX, CGRectGetHeight(contentRect)-2, titleW, 2);
    
    return rect;
#warning 我自定义
    //
    //    CGFloat w = kUIScreen.size.width/3;
    //    CGFloat x = (titleW)/2;
    //    CGFloat y = (CGRectGetHeight(contentRect)) -w-5;
    //    CGFloat h = w;
    //    return CGRectMake(0, 39, titleW, 1);
}
- (CGRect)titleRectWithImageNormalBottomForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)/2*scale;
    CGFloat titleH = [self heightForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    //    CGFloat titleY = (CGRectGetHeight(contentRect)-imageWH-titleH)/2;
#warning 自定义
    CGFloat titleY = (CGRectGetHeight(contentRect)-titleH)/2-10;
    CGRect rect = CGRectMake(0, titleY, CGRectGetWidth(contentRect) , 39);
    
    return rect;
}
#pragma mark imageRight 图片在右 文字在左
- (CGRect)imageRectWithImageRightForContentTect:(CGRect)contentRect{
//    CGFloat imageWH = CGRectGetHeight(contentRect)*scale;
    CGFloat inteval = (CGRectGetHeight(contentRect)-10)/2;
//    CGFloat titleW = [self widthForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat titleW = [self getWidthWithTitle:[self currentTitle] withFontSize:13]+10;
    if (titleW > CGRectGetWidth(contentRect)*0.8) {
        titleW = CGRectGetWidth(contentRect)*0.8;
    }
    CGFloat titleX = (CGRectGetWidth(contentRect)-titleW-10)/2;
    if (titleX < 0) {
        titleX = 0;
    }
    CGRect rect = CGRectMake(titleX+titleW , inteval, 10, 10);
    XLog(@"contentRect:%@",NSStringFromCGRect(contentRect));
    XLog(@"titleW:%f",titleW);
    XLog(@"titleX:%f",titleX);
    return rect;
    
//    #warning 我自定义
//    CGFloat w = 25/2;
//    CGFloat x = (CGRectGetWidth(contentRect))/2 + titleW/2.2;
//    CGFloat h = 16/2;
//    CGFloat y = self.frame.size.height/2 - h/2;
//    return CGRectMake(x, y, w, h);
}

- (CGRect)titleRectWithImageRightForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)*scale;
//    CGFloat titleW = [self widthForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat titleW = [self getWidthWithTitle:[self currentTitle] withFontSize:13]+10;
    XLog(@"[self currentTitle]:%@",[self currentTitle]);
    
    CGFloat titleX = (CGRectGetWidth(contentRect)-titleW-10)/2;
//    #warning 我自定义
//    CGFloat titleX = (CGRectGetWidth(contentRect))/2 - titleW/2;
    if (titleX < 0) {
        titleX = 0;
    }
    if (titleW > CGRectGetWidth(contentRect)*0.8) {
        titleW = CGRectGetWidth(contentRect)*0.8;
        CGRect rect = CGRectMake(titleX, 0, titleW , CGRectGetHeight(contentRect));
         return rect;
    }
    CGRect rect = CGRectMake(titleX, 0, titleW , CGRectGetHeight(contentRect));
    return rect;
    
}
//titleRectWithImageMarginRightForContentTect
- (CGRect)titleRectWithImageMarginRightForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)*scale;
    //    CGFloat titleW = [self widthForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat titleW = [self widthForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    
    CGFloat titleX = (CGRectGetWidth(contentRect)-titleW-imageWH)/2;
    //    #warning 我自定义
    //    CGFloat titleX = (CGRectGetWidth(contentRect))/2 - titleW/2;
    if (titleX < 0) {
        titleX = 0;
    }
    //    if (titleW > CGRectGetWidth(contentRect)*0.8) {
    //        titleW = CGRectGetWidth(contentRect)*0.8;
    //        CGRect rect = CGRectMake(titleX, 0, titleW , CGRectGetHeight(contentRect));
    //        return rect;
    //    }
    CGRect rect = CGRectMake(0, 0, titleW , CGRectGetHeight(contentRect));
    return rect;
    
}
//titleRectWithImageNormalRightForContentTect
- (CGRect)titleRectWithImageNormalRightForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)*scale;
    //    CGFloat titleW = [self widthForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat titleW = [self widthForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    
    CGFloat titleX = (CGRectGetWidth(contentRect)-titleW-imageWH)/2;
    //    #warning 我自定义
    //    CGFloat titleX = (CGRectGetWidth(contentRect))/2 - titleW/2;
    if (titleX < 0) {
        titleX = 0;
    }
//    if (titleW > CGRectGetWidth(contentRect)*0.8) {
//        titleW = CGRectGetWidth(contentRect)*0.8;
//        CGRect rect = CGRectMake(titleX, 0, titleW , CGRectGetHeight(contentRect));
//        return rect;
//    }
    CGRect rect = CGRectMake(titleX, 0, titleW , CGRectGetHeight(contentRect));
    return rect;
    
}
//imageRectWithImageMarginRightForContentTect
- (CGRect)imageRectWithImageMarginRightForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)*scale;
    CGFloat inteval = (CGRectGetHeight(contentRect)-25)/2;
    //    CGFloat titleW = [self widthForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat titleW = [self getWidthWithTitle:[self currentTitle] withFontSize:13];
    if (titleW > CGRectGetWidth(contentRect)*0.8) {
        titleW = CGRectGetWidth(contentRect)*0.8;
    }
    CGFloat titleX = (CGRectGetWidth(contentRect)-titleW-imageWH)/2;
    if (titleX < 0) {
        titleX = 0;
    }
    //    CGRect rect = CGRectMake(titleX+titleW , inteval, imageWH, imageWH);
    //    XLog(@"contentRect:%@",NSStringFromCGRect(contentRect));
    //    XLog(@"titleW:%f",titleW);
    //    XLog(@"titleX:%f",titleX);
    //    return rect;
    
    //    #warning 我自定义
    CGFloat w = 25/2;
    CGFloat x = CGRectGetWidth(contentRect)-imageWH;
    CGFloat h = 16/2;
    CGFloat y = self.frame.size.height/2 - h/2;
    return CGRectMake(x, y, w, h);
}
//imageRectWithImageNormalRightForContentTect
- (CGRect)imageRectWithImageNormalRightForContentTect:(CGRect)contentRect{
    CGFloat imageWH = CGRectGetHeight(contentRect)*scale;
    CGFloat inteval = (CGRectGetHeight(contentRect)-25)/2;
    //    CGFloat titleW = [self widthForTitleString:[self titleForState:UIControlStateNormal] ContentRect:contentRect];
    CGFloat titleW = [self getWidthWithTitle:[self currentTitle] withFontSize:13];
    if (titleW > CGRectGetWidth(contentRect)*0.8) {
        titleW = CGRectGetWidth(contentRect)*0.8;
    }
    CGFloat titleX = (CGRectGetWidth(contentRect)-titleW-imageWH)/2;
    if (titleX < 0) {
        titleX = 0;
    }
//    CGRect rect = CGRectMake(titleX+titleW , inteval, imageWH, imageWH);
//    XLog(@"contentRect:%@",NSStringFromCGRect(contentRect));
//    XLog(@"titleW:%f",titleW);
//    XLog(@"titleX:%f",titleX);
//    return rect;
    
    //    #warning 我自定义
        CGFloat w = 25/2;
        CGFloat x = (CGRectGetWidth(contentRect))/2 + titleW+5;
        CGFloat h = 16/2;
        CGFloat y = self.frame.size.height/2 - h/2;
        return CGRectMake(x, y, w, h);
}
#pragma mark 计算标题内容宽度
- (CGFloat)widthForTitleString:(NSString *)string ContentRect:(CGRect)contentRect{
   if (string) {
       XLog(@"string:%@",string);
        CGSize constraint = contentRect.size;
        NSAttributedString* attributedText = [[NSAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName: self.titleLabel.font}];
        CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
        CGSize size = rect.size;
        CGFloat width = MAX(size.width, 30);
        CGFloat imageW = [self imageForState:UIControlStateNormal].size.width;

        if (width+imageW > CGRectGetWidth(contentRect)) { // 当标题和图片宽度超过按钮宽度时不能越界
           return  CGRectGetWidth(contentRect) - imageW;
        }
        return width+10;
    }
    else {
        return CGRectGetWidth(contentRect)/2;
    }
}

#pragma mark 计算标题文字内容的高度
- (CGFloat)heightForTitleString:(NSString *)string ContentRect:(CGRect)contentRect{
    if (string) {
        CGSize constraint = contentRect.size;
        NSAttributedString* attributedText = [[NSAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName: self.titleLabel.font}];
        CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
        CGSize size = rect.size;
        CGFloat height = MAX(size.height, 5);
        
        if (height > CGRectGetHeight(contentRect)/2) { // 当标题高度超过按钮1/2宽度时
            return  CGRectGetHeight(contentRect)/2 ;
        }
        return height;
    }
    else {
        return CGRectGetHeight(contentRect)/2;
    }
}

+ (CGFloat)getWidthWithTitle:(NSString *)title withFontSize:(CGFloat )fontSize
{
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
//    XLog(@"title:%@",title);
    CGSize size = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:options attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size;
    return size.width;
}
- (CGFloat)getWidthWithTitle:(NSString *)title withFontSize:(CGFloat )fontSize
{
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGSize size = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:options attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size;
    return size.width;
}
@end
